package com.infinit.easyreader.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.infinit.easyreader.R;
import com.infinit.easyreader.adpter.BookmarkAdapter;
import com.infinit.easyreader.dao.BookmarkDao;
import com.infinit.easyreader.entity.Book;
import com.infinit.easyreader.entity.Bookmark;
import com.infinit.easyreader.ui.BookmarkList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类：com.infinit.easyreader.fragment.BookmarkFragment V[1.0.0]
 * 作用：书签列表的fragment
 * Created by rzy on 2016/5/25.
 */
public class BookmarkFragment extends Fragment {


    public static final String MARKED_POSITION = "markedPosition";
    @Bind(R.id.lvMarks)
    ListView lvMarks;
    @Bind(R.id.tvNoMark)
    TextView tvNoMark;
    private int bookId;
    private int bookLength;
    private List<Bookmark> marks;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        bookId = bundle.getInt(BookmarkList.BOOK_ID);
        bookLength = bundle.getInt(BookmarkList.BOOK_LENGTH);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bookmark_list_fragment, container, false);
        ButterKnife.bind(this, view);
        getAllBookmarks();
        setAdapter();
        tvNoMark.setVisibility(View.GONE);
        if (marks.size() == 0) {
            tvNoMark.setVisibility(View.VISIBLE);
        }
        setListeners();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    /**
     * 作用：设置适配器
     */

    private void setAdapter() {
        BookmarkAdapter markAdapter = new BookmarkAdapter(marks, getActivity(), bookLength);
        lvMarks.setAdapter(markAdapter);
    }


    /**
     * 读取数据库，获取所有的书签列表
     */


    private void getAllBookmarks() {
        Book book = new Book();
        book.setBookId(bookId);
        marks = new ArrayList<>();
        BookmarkDao bmDao = new BookmarkDao(getActivity());
        marks = bmDao.findAllBookmark(book);
    }

    /**
     * 设置监听器
     */

    private void setListeners() {

        lvMarks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获取当前的bookmark
                Bookmark bookmark = marks.get(position);
                Activity mActivity = getActivity();
                Intent intent = new Intent();
                int curPosition = (int) bookmark.getPosition();
                intent.putExtra(MARKED_POSITION, curPosition);
                mActivity.setResult(Activity.RESULT_OK, intent);
                mActivity.finish();
            }
        });

    }


}
